﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.CCFlow.EV;
using BEGDC.CC.BL.Common;

namespace BEGDC.CC.Web.UserControls.EV
{
    /// <summary>
    /// Summary description for AddEVMeetingAttendee.
    /// </summary>
    public class AddEVMeetingAttendee : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label lblNameEN;
        protected System.Web.UI.WebControls.Label lblNameCN;
        protected System.Web.UI.WebControls.Label lblCompany;
        protected System.Web.UI.WebControls.TextBox txtNameEN;
        protected System.Web.UI.WebControls.TextBox txtNameCN;
        protected System.Web.UI.WebControls.TextBox txtCompany;
        protected System.Web.UI.WebControls.TextBox txtTitle;
        protected System.Web.UI.WebControls.Button ibtnAddAttendee;
        protected System.Web.UI.WebControls.Label lblPageTitle;
        protected System.Web.UI.WebControls.Label lblTitle;
        protected System.Web.UI.WebControls.Label lblDepartment;
        protected System.Web.UI.WebControls.TextBox txtDepartment;
        protected System.Web.UI.WebControls.Button ibtnClose;
        protected System.Web.UI.HtmlControls.HtmlTableRow TR_Department;

        protected string State
        {
            get { return this.ViewState["MeetingAttendeeState"].ToString(); }
            set { this.ViewState["MeetingAttendeeState"] = value; }
        }


        private int AttendeeID
        {
            get
            {
                if (ViewState["AttendeeID"] == null)
                    return 0;
                else
                {
                    return (int)ViewState["AttendeeID"];
                }
            }
            set
            {
                ViewState["AttendeeID"] = value;
            }
        }

        private string MeetingID
        {
            get
            {
                return Request.QueryString["MeetingID"];
            }
        }
        private string Company
        {
            get
            {
                if (ViewState["Company"] == null)
                    return string.Empty;
                else
                {
                    return ViewState["Company"].ToString(); ;
                }
            }
            set
            {
                ViewState["Company"] = value;
            }

        }
        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPageInfo();
                InitForm();
                this.ibtnAddAttendee.Attributes["onclick"] = "javascript:CheckInfoComplete();";
                this.ibtnClose.Attributes["onclick"] = "javascript:OnClose();";
            }
        }

        #region InitForm
        private void InitForm()
        {
            DataRow row = ObtainAttendeeFromDataTable((DataTable)Session["MeetingAttendee" + this.MeetingID]);
            if (row != null)
            {
                this.txtNameEN.Text = row["NameEN"].ToString();
                this.txtNameCN.Text = row["NameCN"].ToString();
                this.txtCompany.Text = row["Company"].ToString();
                if (this.txtCompany.Text != null && this.txtCompany.Text == "ASB")
                {
                    this.txtCompany.Enabled = false;
                    this.TR_Department.Visible = true;
                    this.txtDepartment.Text = row["Department"].ToString();
                }
                else
                {
                    this.TR_Department.Visible = false;
                }
                this.txtTitle.Text = row["Title"].ToString();
            }
            else
            {
                if (this.txtCompany.Text != null && this.txtCompany.Text == "ASB")
                {
                    this.txtCompany.Enabled = false;
                    this.TR_Department.Visible = true;
                }
                else
                {
                    this.TR_Department.Visible = false;
                }
            }
        }
        #endregion

        #region ObtainAttendeeFromDataTable
        private DataRow ObtainAttendeeFromDataTable(DataTable datatable)
        {
            if (datatable == null || datatable.Rows.Count == 0 || this.AttendeeID == 0) return null;

            string IdStr = null;
            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                IdStr = datatable.Rows[i]["RowId"].ToString();
                int IdInt = Convert.ToInt32(IdStr);
                if (IdInt == this.AttendeeID)
                {
                    return datatable.Rows[i];
                }
            }
            return null;
        }
        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.ibtnAddAttendee.Click += new EventHandler(ibtnAddAttendee_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void InitPageInfo()
        {

            if (Request.QueryString["Company"] + string.Empty != string.Empty)
                this.Company = Request.QueryString["Company"].ToString();

            //Jovi update 8/20
            //			if(this.Company!=null && this.Company == "ASB")
            //			{
            //				this.txtCompany.Enabled = false;
            //			}
            //			else
            //			{
            //				this.TR_Department.Visible = false;
            //			}

            if (Request.QueryString["AttendeeID"] + string.Empty != string.Empty)
            {
                this.lblPageTitle.Text = "Edit Attendee";
                this.AttendeeID = Convert.ToInt32(Request.QueryString["AttendeeID"]);
            }
            this.State = Request["State"];

            this.txtCompany.Text = this.Company;
        }

        private void InitAttendeeInfo()
        {
            if (this.AttendeeID > 0)
            {
                DataRow row = ObtainAttendeeFromDataTable((DataTable)Session["MeetingAttendee" + this.MeetingID]);
                if (row != null)
                {
                    this.txtCompany.Text = row["Company"].ToString();
                    this.txtNameEN.Text = row["NameEN"].ToString();
                    this.txtNameCN.Text = row["NameCN"].ToString();
                    this.txtTitle.Text = row["Title"].ToString();
                }
            }
        }

        #region Update Meeting Minutes Attendee Session
        private void UpdateAttendeeSession()
        {
            DataTable AttendeeDataTable = null;
            DataRow AttendeeRow = null;
            object SessAcatelAttendee = Session["MeetingAttendee" + this.MeetingID];
            if (SessAcatelAttendee != null)
            {
                AttendeeDataTable = (DataTable)SessAcatelAttendee;
            }
            else
            {
                return;
            }

            if (this.AttendeeID == 0)
            {
                AttendeeRow = AttendeeDataTable.NewRow();
                AttendeeRow["AttendeeID"] = AttendeeDataTable.Rows.Count + 1;
                AttendeeRow["NameCN"] = this.txtNameCN.Text;
                AttendeeRow["NameEN"] = this.txtNameEN.Text;
                AttendeeRow["Company"] = this.txtCompany.Text;
                if (this.txtCompany.Text != null && this.txtCompany.Text == "ASB")
                    AttendeeRow["Department"] = this.txtDepartment.Text;
                AttendeeRow["Title"] = this.txtTitle.Text;

                AttendeeDataTable.Rows.Add(AttendeeRow);
            }
            else
            {
                AttendeeRow = ObtainAttendeeFromDataTable(AttendeeDataTable);
                AttendeeRow["NameEN"] = this.txtNameEN.Text;
                AttendeeRow["NameCN"] = this.txtNameCN.Text;
                AttendeeRow["Title"] = this.txtTitle.Text;
                AttendeeRow["Company"] = this.txtCompany.Text;
                if (this.txtCompany.Text != null && this.txtCompany.Text == "ASB")
                    AttendeeRow["Department"] = this.txtDepartment.Text;
            }


            Session["MeetingAttendee" + this.MeetingID] = SetRowID(AttendeeDataTable);
        }

        private DataTable SetRowID(DataTable table)
        {
            if (table != null && table.Rows.Count > 0)
                for (int i = 1; i <= table.Rows.Count; i++)
                {
                    table.Rows[i - 1][0] = i.ToString();
                }
            return table;
        }

        #endregion

        void ibtnAddAttendee_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateAttendeeSession();
            }
            catch (Exception exp)
            {
                throw exp;
            }
            Response.Write("<script>window.close();window.returnValue='reload';</script>");
        }


    }
}
